<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="../vue.global.js"></script>
</head>

<body>
    <div id="app">
        <p>Has published books:</p>

        <!-- 访问computed函数返回的值 -->
        <span>{{ publishedBooksMessage }}</span>

        <hr>
        <ul>
            <li v-for="book in author.books" :key="book">
                {{book}}
            </li>
        </ul>
        <button @click="publishABook">出版一本书</button>
        <hr>
        {{counter}}
        <button @click="counter++">counter++</button>
    </div>

    <script>
        Vue.createApp({
            data() {
                return {
                    counter: 0,
                    author: {
                        name: 'John Doe',
                        books: [
                            'Vue 2 - Advanced Guide',
                            'Vue 3 - Basic Guide',
                            'Vue 4 - The Mystery'
                        ]
                    }
                }
            },
            computed: {
                // 所依赖的一手数据项this.author.books如果发生变化 重新计算
                // 如果没有变化 则不重新计算
                // 计算属性的 getter
                publishedBooksMessage() {
                    console.log("publishedBooksMessage");
                    // `this` 指向 vm 实例
                    return this.author.books.length > 0 ? 'Yes' : 'No'
                }
            },
            methods: {
                publishABook() {
                    this.author.books.push("金瓶梅")
                }
            }
        }).mount('#app')
    </script>
</body>

</html>